//
//  PurchaseDetailViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/26.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "PurchaseDetailViewController.h"
#import "ModifyAddressViewController.h"
#import "PayWebViewController.h"
#import "AppDelegate.h"

#import "StepButton.h"

#import <UIImageView+WebCache.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "HWZBAPIRequest.h"
#import "MBProgressHUD+Toast.h"
#import "SelectButton.h"
#import "Context.h"
#import "ProviderPickerView.h"
#import "NSString+Utils.h"


@interface PurchaseDetailViewController () <ProviderPickerViewDelegate, StepButtonDelegate>

@property (strong, nonatomic) UIView *packageView;
@property (strong, nonatomic) StepButton *numButton;                  //数量按钮

@property (strong, nonatomic) UITextField *mobilePhoneTextField;      //充值手机号
@property (strong, nonatomic) UITextField *serviceProviderTextField;  //运营商
@property (strong, nonatomic) ProviderPickerView *pickerView;

@property (strong, nonatomic) UITextField *holderNameTextField;       //持卡人姓名
@property (strong, nonatomic) UITextField *oilCardNoTextField;        //油卡卡号

@property (strong, nonatomic) UIButton *addressButton;
@property (strong, nonatomic) UITextField *addressTextField;          //地址

@property (strong, nonatomic) UILabel *amountLabel;                   //金额(或苹果数)
@property (strong, nonatomic) UILabel *totalLabel;                    //总计
@property (strong, nonatomic) SelectButton *alipayButton;             //支付宝支付
@property (strong, nonatomic) SelectButton *hwpayButton;              //铧为钻石苹果支付

@property (assign, nonatomic) NSInteger commodityNum;                 //商品数量
@property (assign, nonatomic) NSInteger discountPrice;                //折扣价
@property (assign, nonatomic) NSInteger totalPrice;                   //总价
@property (strong, nonatomic) UIButton *payButton;                    //立即支付按钮
@property (strong, nonatomic) NSString *payMethod;                    //支付宝或者铧为
@property (strong, nonatomic) NSString *providerTypeId;               //运营商类型

@end

@implementation PurchaseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"购买详情";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginNotifyAction:) name:kNotificationUserLogin object:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if([Context sharedInstance].recipientsAddress) {
        self.addressTextField.text = [Context sharedInstance].recipientsAddress;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)userLoginNotifyAction:(NSNotification *)notification {
    //用户登录刷新UI
    
}

#pragma mark - ProviderPickerViewDelegate
- (void)pickerView:(ProviderPickerView *)pickerView didSelectAtProvider:(NSDictionary *)provider {
    NSString *providerName = provider[@"provider"];
    NSString *providerId = provider[@"providerId"];
    self.providerTypeId = providerId;
    NSLog(@"%@", providerId);
    self.serviceProviderTextField.text = providerName;
}

#pragma mark - StepButtonDelegate
- (void)button:(StepButton *)button clickedWithValue:(NSInteger)value {
    NSInteger combinePrice = [self.packageInfo.combinePrice integerValue];
    NSString *amountStr = [NSString stringWithFormat:@"应付: ￥%ld\n立减: -￥%ld", (combinePrice * value), _discountPrice];
    self.amountLabel.text = amountStr;
    NSInteger totalPrice = combinePrice * value - _discountPrice ;
    NSString *totalStr = [NSString stringWithFormat:@"合计: ￥%ld", totalPrice];
    self.totalLabel.text = totalStr;
    self.totalPrice = totalPrice;
    self.commodityNum = value;
}

#pragma mark - UIButton Action

- (void)serviceProviderTapAction {
    [self.mobilePhoneTextField resignFirstResponder];
    if(self.pickerView.hidden) {
        self.pickerView.hidden = NO;
    } else {
        self.pickerView.hidden = YES;
    }
}
- (void)addressTapAction:(id)sender {
    ModifyAddressViewController *modifyAddressViewController = [[ModifyAddressViewController alloc] init];
    [self.navigationController pushViewController:modifyAddressViewController animated:YES];
}

- (void)selectPayMethonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    if(button == self.alipayButton) {
        [self.alipayButton setSelected:YES];
        [self.hwpayButton setSelected:NO];
        self.payMethod = @"1";
    } else {
        [self.alipayButton setSelected:NO];
        [self.hwpayButton setSelected:YES];
        self.payMethod = @"2";
    }
}

- (void)payButtonAction:(id)sender {
    self.editing = NO;
    BOOL hasLogin = [Context sharedInstance].isLogin;
    if(!hasLogin) {
        [MBProgressHUD showToast:@"用户登录后才能购买商品, 请先登录" initWithView:self.view];
        [[AppDelegate sharedDelegate] showLoginViewController];
        return;
    }
    if([self.commodityInfo.typeId isEqualToString:@"1"]) {
        //油卡
        NSString *cardOwner = self.holderNameTextField.text;
        if([NSString isBlankString:cardOwner]) {
            [MBProgressHUD showToast:@"请输入持卡人姓名" initWithView:self.view];
            return;
        }
        NSString *cardNoStr = self.oilCardNoTextField.text;
        if([NSString isBlankString:cardNoStr]) {
            [MBProgressHUD showToast:@"请输入油卡卡号" initWithView:self.view];
            return;
        } else if(cardNoStr.length < 16 || cardNoStr.length > 19) {
            [MBProgressHUD showToast:@"请输入正确的油卡卡号, 卡号长度为16~19位" initWithView:self.view];
            return;
        }
        NSString *areaId = [Context sharedInstance].recipientsCountyId;
        NSString *areaStr = [Context sharedInstance].recipientsArea;
        if([NSString isBlankString:areaId] || [NSString isBlankString:areaStr]) {
            [MBProgressHUD showToast:@"请输入收货地址信息" initWithView:self.view];
            return;
        }
    } else if([self.commodityInfo.typeId isEqualToString:@"2"]) {
        //充值卡
        NSString *mobile = self.mobilePhoneTextField.text;
        if([NSString isBlankString:mobile]) {
            [MBProgressHUD showToast:@"请输入手机号" initWithView:self.view];
            return;
        } else if(![RegexKit validateMobile:mobile]) {
            [MBProgressHUD showToast:@"请输入正确的手机号" initWithView:self.view];
            return;
        }
        NSString *providerName = self.serviceProviderTextField.text;
        if([NSString isBlankString:providerName]) {
            [MBProgressHUD showToast:@"请选择运营商类型" initWithView:self.view];
            return;
        }
    } else if([self.commodityInfo.typeId isEqualToString:@"3"]) {
        //手机
        NSString *areaId = [Context sharedInstance].recipientsCountyId;
        NSString *areaStr = [Context sharedInstance].recipientsArea;
        if([NSString isBlankString:areaId] || [NSString isBlankString:areaStr]) {
            [MBProgressHUD showToast:@"请输入收货地址信息" initWithView:self.view];
            return;
        }
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    NSString *loginId = [Context sharedInstance].loginID;
    [params setObject:loginId forKey:@"loginid"];
    NSString *accountId = [Context sharedInstance].accountId;
    [params setObject:accountId forKey:@"accountId"];
    //商品ID
    NSString *productId = self.commodityInfo.commodityId;
    [params setObject:productId forKey:@"productId"];
    NSString *commodityNum = [NSString stringWithFormat:@"%ld", self.commodityNum];
    [params setObject:commodityNum forKey:@"productQuantity"];
    /* 支付方式(1:支付宝、2：铧为钻石苹果) */
    [params setObject:self.payMethod forKey:@"payModel"];
    /* 系统代码(1:Android、2:IOS) */
    [params setObject:@"2" forKey:@"osModel"];
    NSString *packageId = self.packageInfo.packageId;
    //套餐唯一标识
    [params setObject:packageId forKey:@"modelId"];
    //套餐价格
    NSString *packagePrice = self.packageInfo.combinePrice;
    [params setObject:packagePrice forKey:@"productPrice"];
    //套餐折扣价
    NSString *discountPriceStr = [NSString stringWithFormat:@"%ld", self.discountPrice];
    [params setObject:discountPriceStr forKey:@"discountPrice"];
    //套餐数量
    NSString *packageNumStr = [NSString stringWithFormat:@"%ld", self.commodityNum];
    [params setObject:packageNumStr forKey:@"productQuantity"];
    //应付金额
    NSString *priceStr = [NSString stringWithFormat:@"%ld", [packagePrice integerValue] * _commodityNum - _discountPrice];
    [params setObject:priceStr forKey:@"payPrice"];
    //购买状态(0:购买中、1：交易成功、2：订单成功、3：订单失败)
    [params setObject:@"0" forKey:@"state"];
    //判断
    if([self.commodityInfo.typeId isEqualToString:@"1"]) {
        //油卡
        NSMutableDictionary *oilDict = [NSMutableDictionary dictionaryWithCapacity:0];
        NSString *cardNoStr = self.oilCardNoTextField.text;
        [oilDict setObject:cardNoStr forKey:@"cardNumber"];
        NSString *cardOwner = self.holderNameTextField.text;
        [oilDict setObject:cardOwner forKey:@"cardOwnerName"];
        [oilDict setObject:self.commodityInfo.typeId forKey:@"cardType"];
        NSString *areaId = [Context sharedInstance].recipientsCountyId;
        NSString *areaStr = [Context sharedInstance].recipientsArea;
        [oilDict setObject:areaId forKey:@"areaId"];
        [oilDict setObject:areaStr forKey:@"deliveryAddress"];
        [params setObject:oilDict forKey:@"oil"];
    } else if([self.commodityInfo.typeId isEqualToString:@"2"]) {
        //充值卡
        NSMutableDictionary *cardDict = [NSMutableDictionary dictionaryWithCapacity:0];
        NSString *providerName = self.serviceProviderTextField.text;
        [cardDict setObject:providerName forKey:@"operatorType"];
        NSString *mobile = self.mobilePhoneTextField.text;
        [cardDict setObject:mobile forKey:@"mobile"];
        [cardDict setObject:@"" forKey:@"deliveryName"];
        [cardDict setObject:@"" forKey:@"deliveryMobile"];
//        NSString *areaId = [Context sharedInstance].recipientsCountyId != nil ? [Context sharedInstance].recipientsCountyId : @"";
        //        [cardDict setObject:areaId forKey:@"areaId"];
        NSString *areaStr = [Context sharedInstance].recipientsArea != nil ? [Context sharedInstance].recipientsArea : @"";
        [cardDict setObject:areaStr forKey:@"deliveryAddress"];
        [cardDict setObject:self.commodityInfo.commodityId forKey:@"productId"];
        [params setObject:cardDict forKey:@"card"];
    } else if([self.commodityInfo.typeId isEqualToString:@"3"]) {
        //手机
        NSMutableDictionary *phoneDict = [NSMutableDictionary dictionaryWithCapacity:0];
        NSString *areaId = [Context sharedInstance].recipientsCountyId;
        NSString *areaStr = [Context sharedInstance].recipientsArea;
        [phoneDict setObject:areaId forKey:@"areaId"];
        [phoneDict setObject:areaStr forKey:@"deliveryAddress"];
        [params setObject:phoneDict forKey:@"phone"];
    }
    [SVProgressHUD showWithStatus:@"正在提交订单" maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] commitOrderWithParams:params success:^(OrderDetailInfo *detailInfo) {
        [SVProgressHUD dismiss];
        if(detailInfo != nil && detailInfo.orderId) {
            NSString *orderId = detailInfo.orderId;
            [self payForOrder:orderId];
        } else {
            [MBProgressHUD showToast:@"提交订单失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)payForOrder:(NSString *)orderId {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:orderId forKey:@"id"];
    NSInteger packagePrice = [self.packageInfo.combinePrice integerValue];
    NSString *priceStr = [NSString stringWithFormat:@"%ld", packagePrice * _commodityNum - _discountPrice];
    [params setObject:priceStr forKey:@"payPrice"];
    /* 支付方式(1:支付宝、2：铧为钻石苹果) */
    [params setObject:_payMethod forKey:@"payModel"];
    [[HWZBAPIRequest sharedInstance] payForOrderWithParams:params success:^(NSString *result) {
        if([_payMethod isEqualToString:@"1"]) {
            PayWebViewController *payWebViewController = [[PayWebViewController alloc] init];
            payWebViewController.htmlStr = result;
            [self.navigationController pushViewController:payWebViewController animated:YES];
        }  else {
            [MBProgressHUD showToast:@"支付成功!" initWithView:self.view];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRefreshOrder object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAccountInfoChange object:nil];
            [self performSelector:@selector(popToRootViewController) withObject:nil afterDelay:1.5f];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)popToRootViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

#pragma mark - Private Method
- (void)createUI {
    //套餐视图
    self.packageView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(0.0f), SCREEN_WIDTH, TRANS_VALUE(90.0f))];
    self.packageView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:self.packageView];
    [self createPackageView:self.packageView];
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(90.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView1];
    
    UIView *sectionView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(90.0f), SCREEN_WIDTH, TRANS_VALUE(50.0f))];
    sectionView2.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:sectionView2];
    [self createSectionView2:sectionView2];
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(140.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView2.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView2];
    if([self.self.commodityInfo.typeId isEqualToString:@"1"]) {
        //油卡
        UIView *sectionView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(140.0f), SCREEN_WIDTH, TRANS_VALUE(80.0f))];
        sectionView3.backgroundColor = I_COLOR_WHITE;
        [self createOilCardView:sectionView3];
        [self.view addSubview:sectionView3];
        
        UIView *dividerView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(220.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
        dividerView3.backgroundColor = I_COLOR_DIVIDER;
        [self.view addSubview:dividerView3];
        //地址信息
        self.addressButton = [[UIButton alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(220.0f), SCREEN_WIDTH, TRANS_VALUE(40.0f))];
        self.addressButton.backgroundColor = I_COLOR_WHITE;
        [self createAddressView:self.addressButton];
        [self.view addSubview:self.addressButton];
        [self.addressButton addTarget:self action:@selector(addressTapAction:) forControlEvents:UIControlEventTouchUpInside];
        UIView *dividerView5 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(260.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
        dividerView5.backgroundColor = I_COLOR_DIVIDER;
        [self.view addSubview:dividerView5];
        //支付页面
        UIView *payView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(260.0f), SCREEN_WIDTH, TRANS_VALUE(145.0f))];
        payView.backgroundColor = I_COLOR_WHITE;
        [self createPayView:payView];
        [self.view addSubview:payView];
        
    } else if([self.commodityInfo.typeId isEqualToString:@"2"]) {
        //充值卡
        UIView *sectionView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(140.0f), SCREEN_WIDTH, TRANS_VALUE(80.0f))];
        sectionView3.backgroundColor = I_COLOR_WHITE;
        [self createRechargeCardView:sectionView3];
        [self.view addSubview:sectionView3];
        
        UIView *dividerView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(220.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
        dividerView3.backgroundColor = I_COLOR_DIVIDER;
        [self.view addSubview:dividerView3];
        //支付页面
        UIView *payView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(220.0f), SCREEN_WIDTH, TRANS_VALUE(145.0f))];
        payView.backgroundColor = I_COLOR_WHITE;
        [self createPayView:payView];
        [self.view addSubview:payView];
    } else if([self.commodityInfo.typeId isEqualToString:@"3"]) {
        //手机
        //地址信息
        self.addressButton = [[UIButton alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(140.0f), SCREEN_WIDTH, TRANS_VALUE(40.0f))];
        self.addressButton.backgroundColor = I_COLOR_WHITE;
        [self createAddressView:self.addressButton];
        [self.view addSubview:self.addressButton];
        [self.addressButton addTarget:self action:@selector(addressTapAction:) forControlEvents:UIControlEventTouchUpInside];
        UIView *dividerView5 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(180.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
        dividerView5.backgroundColor = I_COLOR_DIVIDER;
        [self.view addSubview:dividerView5];
        //支付页面
        UIView *payView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(180.0f), SCREEN_WIDTH, TRANS_VALUE(145.0f))];
        payView.backgroundColor = I_COLOR_WHITE;
        [self createPayView:payView];
        [self.view addSubview:payView];
    }
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 64.0f - TRANS_VALUE(60.0f), SCREEN_WIDTH, TRANS_VALUE(44.0f))];
    footerView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:footerView];
    
    UIView *dividerView6 = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH, 1.0f)];
    dividerView6.backgroundColor = I_COLOR_DIVIDER;
    [footerView addSubview:dividerView6];
    
    self.amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(5.0f), TRANS_VALUE(0.0f), TRANS_VALUE(75.0f), TRANS_VALUE(60.0f))];
    self.amountLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(10.0f)];
    self.amountLabel.textAlignment = NSTextAlignmentLeft;
    self.amountLabel.numberOfLines = 0;
    self.amountLabel.textColor = I_COLOR_DARKGRAY;
    self.amountLabel.text = @"应付: ￥3000\n立减: -￥20";
    [footerView addSubview:self.amountLabel];
    
    UIView *dividerView7 = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(80.0f), TRANS_VALUE(5.0f), 1.0f, TRANS_VALUE(50.0f))];
    dividerView7.backgroundColor = I_COLOR_DIVIDER;
    [footerView addSubview:dividerView7];
    
    self.totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(85.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - TRANS_VALUE(210.0f), TRANS_VALUE(60.0f))];
    self.totalLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    self.totalLabel.textAlignment = NSTextAlignmentLeft;
    self.totalLabel.textColor = I_COLOR_BLACK;
    self.totalLabel.text = @"合计: ￥2980";
    [footerView addSubview:self.totalLabel];
    
    self.payButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TRANS_VALUE(120.0f), TRANS_VALUE(0.0f), TRANS_VALUE(120.0f), TRANS_VALUE(60.0f))];
    self.payButton.backgroundColor = I_COLOR_RED;
    self.payButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.payButton setTitle:@"立即支付" forState:UIControlStateNormal];
    [self.payButton setTitle:@"立即支付" forState:UIControlStateSelected];
    [self.payButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.payButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [footerView addSubview:self.payButton];
    
    [self.payButton addTarget:self action:@selector(payButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.pickerView = [[ProviderPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 64.0f - 240.0f, SCREEN_WIDTH, 240.0f)];
    [self.view addSubview:self.pickerView];
    self.pickerView.delegate = self;
    self.pickerView.hidden = YES;
}

- (void)createPackageView:(UIView *)sectionView {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(230.0f), TRANS_VALUE(30.0f))];
    titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    titleLabel.textColor = I_COLOR_33BLACK;
    titleLabel.text = @"优惠套餐1";
    [sectionView addSubview:titleLabel];
    titleLabel.tag = 40001;
    
    UILabel *sumLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(240.0f), TRANS_VALUE(0.0f), TRANS_VALUE(60.0f), TRANS_VALUE(30.0f))];
    sumLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    sumLabel.textAlignment = NSTextAlignmentRight;
    sumLabel.textColor = I_COLOR_33BLACK;
    sumLabel.text = @"共3件";
    [sectionView addSubview:sumLabel];
    sumLabel.tag = 40002;
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(310.0f), TRANS_VALUE(10.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    [sectionView addSubview:arrowImageView];
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
    
    UIImageView *pictureImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
    pictureImageView1.contentMode = UIViewContentModeScaleAspectFit;
    pictureImageView1.clipsToBounds = YES;
    pictureImageView1.layer.borderColor = I_COLOR_GRAY.CGColor;
    pictureImageView1.layer.borderWidth = 0.0f;
    [sectionView addSubview:pictureImageView1];
    pictureImageView1.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
    pictureImageView1.tag = 40003;
    
    UILabel *add1Label = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(30.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f))];
    add1Label.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
    add1Label.textAlignment = NSTextAlignmentCenter;
    add1Label.textColor = I_COLOR_33BLACK;
    add1Label.text = @"+";
    [sectionView addSubview:add1Label];
    add1Label.tag = 40004;
    
    UIImageView *pictureImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(90.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
    pictureImageView2.contentMode = UIViewContentModeScaleAspectFit;
    pictureImageView2.clipsToBounds = YES;
    pictureImageView2.layer.borderColor = I_COLOR_GRAY.CGColor;
    pictureImageView2.layer.borderWidth = 0.0f;
    [sectionView addSubview:pictureImageView2];
    pictureImageView2.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
    pictureImageView2.tag = 40005;
    
    UILabel *add2Label = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(140.0f), TRANS_VALUE(30.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f))];
    add2Label.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
    add2Label.textAlignment = NSTextAlignmentCenter;
    add2Label.textColor = I_COLOR_33BLACK;
    add2Label.text = @"+";
    [sectionView addSubview:add2Label];
    add2Label.tag = 40006;
    
    UIImageView *pictureImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(170.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
    pictureImageView3.contentMode = UIViewContentModeScaleAspectFit;
    pictureImageView3.clipsToBounds = YES;
    pictureImageView3.layer.borderColor = I_COLOR_GRAY.CGColor;
    pictureImageView3.layer.borderWidth = 0.0f;
    [sectionView addSubview:pictureImageView3];
    pictureImageView3.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
    pictureImageView3.tag = 40007;
    
    UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(220.0f), TRANS_VALUE(30.0f), TRANS_VALUE(80.0f), TRANS_VALUE(50.0f))];
    moneyLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
    moneyLabel.textAlignment = NSTextAlignmentRight;
    moneyLabel.numberOfLines = 0;
    moneyLabel.textColor = I_COLOR_RED;
    moneyLabel.text = @"套餐价:\n ￥3000";
    [sectionView addSubview:moneyLabel];
    moneyLabel.tag = 40008;
}

- (void)createSectionView2:(UIView *)sectionView {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(50.0f))];
    titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    titleLabel.textColor = I_COLOR_33BLACK;
    titleLabel.text = @"数量";
    [sectionView addSubview:titleLabel];
    
    self.numButton = [[StepButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TRANS_VALUE(104.0f), TRANS_VALUE(12.0f), TRANS_VALUE(94.0f), TRANS_VALUE(26.0f))];
    [sectionView addSubview:self.numButton];
}

- (void)createOilCardView:(UIView *)sectionView {
    UILabel *holdernNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    holdernNameLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    holdernNameLabel.textColor = I_COLOR_BLACK;
    holdernNameLabel.text = @"持卡人姓名";
    [sectionView addSubview:holdernNameLabel];
    
    self.holderNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(0.0f), TRANS_VALUE(210.0f), TRANS_VALUE(40.0f))];
    self.holderNameTextField.borderStyle = UITextBorderStyleNone;
    self.holderNameTextField.backgroundColor = I_COLOR_WHITE;
    self.holderNameTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.holderNameTextField.textColor = I_COLOR_BLACK;
    self.holderNameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入持卡人姓名" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.holderNameTextField.tintColor = I_COLOR_BLACK;
    self.holderNameTextField.textAlignment = NSTextAlignmentLeft;
    [sectionView addSubview:self.holderNameTextField];
    
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView.backgroundColor = I_COLOR_DIVIDER;
    [sectionView addSubview:dividerView];
    
    UILabel *oilCardNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(40.0f), TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    oilCardNoLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    oilCardNoLabel.textColor = I_COLOR_BLACK;
    oilCardNoLabel.text = @"油卡卡号";
    [sectionView addSubview:oilCardNoLabel];
    
    self.oilCardNoTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(40.0f), TRANS_VALUE(210.0f), TRANS_VALUE(40.0f))];
    self.oilCardNoTextField.borderStyle = UITextBorderStyleNone;
    self.oilCardNoTextField.backgroundColor = I_COLOR_WHITE;
    self.oilCardNoTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.oilCardNoTextField.textColor = I_COLOR_BLACK;
    self.oilCardNoTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入油卡卡号" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.oilCardNoTextField.tintColor = I_COLOR_BLACK;
    self.oilCardNoTextField.textAlignment = NSTextAlignmentLeft;
    [sectionView addSubview:self.oilCardNoTextField];
    self.oilCardNoTextField.keyboardType = UIKeyboardTypeNumberPad;
}

- (void)createRechargeCardView:(UIView *)sectionView {
    UILabel *holdernNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    holdernNameLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    holdernNameLabel.textColor = I_COLOR_BLACK;
    holdernNameLabel.text = @"充值手机号";
    [sectionView addSubview:holdernNameLabel];
    
    self.mobilePhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(0.0f), TRANS_VALUE(210.0f), TRANS_VALUE(40.0f))];
    self.mobilePhoneTextField.borderStyle = UITextBorderStyleNone;
    self.mobilePhoneTextField.backgroundColor = I_COLOR_WHITE;
    self.mobilePhoneTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.mobilePhoneTextField.textColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.mobilePhoneTextField.tintColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.textAlignment = NSTextAlignmentLeft;
    [sectionView addSubview:self.mobilePhoneTextField];
    self.mobilePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView.backgroundColor = I_COLOR_DIVIDER;
    [sectionView addSubview:dividerView];
    
    UILabel *oilCardNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(40.0f), TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    oilCardNoLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    oilCardNoLabel.textColor = I_COLOR_BLACK;
    oilCardNoLabel.text = @"运营商";
    [sectionView addSubview:oilCardNoLabel];
    
    self.serviceProviderTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(40.0f), TRANS_VALUE(200.0f), TRANS_VALUE(40.0f))];
    self.serviceProviderTextField.borderStyle = UITextBorderStyleNone;
    self.serviceProviderTextField.backgroundColor = I_COLOR_WHITE;
    self.serviceProviderTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.serviceProviderTextField.textColor = I_COLOR_BLACK;
    self.serviceProviderTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请选择运营商类型" attributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
    self.serviceProviderTextField.tintColor = I_COLOR_BLACK;
    self.serviceProviderTextField.textAlignment = NSTextAlignmentLeft;
    [sectionView addSubview:self.serviceProviderTextField];
    self.serviceProviderTextField.enabled = NO;
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(308.0f), TRANS_VALUE(40.0f) + TRANS_VALUE(15.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    [sectionView addSubview:arrowImageView];
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
    self.serviceProviderTextField.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapAction  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(serviceProviderTapAction)];
    [sectionView addGestureRecognizer:tapAction];
}

- (void)createAddressView:(UIView *)sectionView {
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    addressLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    addressLabel.textColor = I_COLOR_BLACK;
    addressLabel.text = @"收货信息";
    [sectionView addSubview:addressLabel];
    
    self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(0.0f), TRANS_VALUE(204.0f), TRANS_VALUE(40.0f))];
    self.addressTextField.borderStyle = UITextBorderStyleNone;
    self.addressTextField.backgroundColor = I_COLOR_WHITE;
    self.addressTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.addressTextField.textColor = I_COLOR_BLACK;
    self.addressTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入收货地址信息" attributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
    self.addressTextField.tintColor = I_COLOR_BLACK;
    self.addressTextField.textAlignment = NSTextAlignmentLeft;
    self.addressTextField.enabled = NO;
    [sectionView addSubview:self.addressTextField];

    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(308.0f), TRANS_VALUE(15.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    [sectionView addSubview:arrowImageView];
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
}

- (void)createPayView:(UIView *)sectionView {
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(5.0f), TRANS_VALUE(100.0f), TRANS_VALUE(20.0f))];
    titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    titleLabel.textColor = I_COLOR_BLACK;
    titleLabel.text = @"支付方式";
    [sectionView addSubview:titleLabel];
    
    self.alipayButton = [[SelectButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(25.0f), SCREEN_WIDTH, TRANS_VALUE(60.0f))];
    self.alipayButton.backgroundColor = I_COLOR_WHITE;
    [self.alipayButton setImage:@"ic_alipay"];
    [self.alipayButton setTitle:@"支付宝"];
    [self.alipayButton setDescription:@"亿万用户都在用, 安全可托付"];
    [sectionView addSubview:self.alipayButton];
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(85.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [sectionView addSubview:dividerView1];
    
    self.hwpayButton = [[SelectButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(85.0f), SCREEN_WIDTH , TRANS_VALUE(60.0f))];
    self.hwpayButton.backgroundColor = I_COLOR_WHITE;
    [self.hwpayButton setImage:@"ic_hwzbpay"];
    [self.hwpayButton setTitle:@"铧为至宝钻石账户"];
    [self.hwpayButton setDescription:@"至尊VIP, 专有账户"];
    [sectionView addSubview:self.hwpayButton];

    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(145.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView2.backgroundColor = I_COLOR_DIVIDER;
    [sectionView addSubview:dividerView2];
    
    [self.alipayButton addTarget:self action:@selector(selectPayMethonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.hwpayButton addTarget:self action:@selector(selectPayMethonAction:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)loadData {
    //初始化
    [Context sharedInstance].recipientsProvinceId = nil;
    [Context sharedInstance].recipientsCityId = nil;
    [Context sharedInstance].recipientsCountyId = nil;
    [Context sharedInstance].recipientsArea = nil;
    
    [self loadPackageInfoInSectionView:self.packageView];
    
    self.numButton.minimumValue = 1;
    self.numButton.maximumValue = 11;
    self.numButton.delegate = self;
    
    //默认支付方式为支付宝
    [self.alipayButton setSelected:YES];
    [self.hwpayButton setSelected:NO];
    self.payMethod = @"1";
    
    //价格显示
    NSInteger combinePrice = [self.packageInfo.combinePrice integerValue];
    if(combinePrice < 2) {
        self.discountPrice = arc4random() % combinePrice;
    } else {
        self.discountPrice = 1 + arc4random() % 2;
    }
    self.commodityNum = 1;
    
    NSString *amountStr = [NSString stringWithFormat:@"应付: ￥%ld\n立减: -￥%ld", combinePrice, _discountPrice];
    self.amountLabel.text = amountStr;
    NSInteger totalPrice = combinePrice - _discountPrice;
    NSString *totalStr = [NSString stringWithFormat:@"合计: ￥%ld", totalPrice];
    self.totalLabel.text = totalStr;
    
}

- (void)loadPackageInfoInSectionView:(UIView *)sectionView {
    UILabel *titleLabel = (UILabel *)[sectionView viewWithTag:40001];
    UILabel *sumLabel = (UILabel *)[sectionView viewWithTag:40002];
    UIImageView *pictureImageView1 = (UIImageView *)[sectionView viewWithTag:40003];
    UILabel *add1Label = (UILabel *)[sectionView viewWithTag:40004];
    UIImageView *pictureImageView2 = (UIImageView *)[sectionView viewWithTag:40005];
    UILabel *add2Label = (UILabel *)[sectionView viewWithTag:40006];
    UIImageView *pictureImageView3 = (UIImageView *)[sectionView viewWithTag:40007];
    UILabel *moneyLabel = (UILabel *)[sectionView viewWithTag:40008];
    if(self.packageInfo) {
        titleLabel.text = self.packageInfo.packageDesc;
        pictureImageView1.hidden = YES;
        add1Label.hidden = YES;
        pictureImageView2.hidden = YES;
        add2Label.hidden = YES;
        pictureImageView3.hidden = YES;
        NSMutableArray *imageUrlArray = [NSMutableArray arrayWithCapacity:0];
        
        if(self.packageInfo.productArray != nil) {
            for(int i = 0, n = (int)self.packageInfo.productArray.count; i < n; i++) {
                ProductInfo *productInfo = [self.packageInfo.productArray objectAtIndex:i];
                if(productInfo.imageUrl != nil) {
                    [imageUrlArray addObject:productInfo.imageUrl];
                }
            }
        }
        if(imageUrlArray.count > 2) {
            pictureImageView1.hidden = NO;
            [pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            add1Label.hidden = NO;
            pictureImageView2.hidden = NO;
            [pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            add2Label.hidden = NO;
            pictureImageView3.hidden = NO;
            [pictureImageView3 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:2]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else if(imageUrlArray.count == 2) {
            pictureImageView1.hidden = NO;
            [pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            add1Label.hidden = NO;
            pictureImageView2.hidden = NO;
            [pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else if(imageUrlArray.count == 1) {
            pictureImageView1.hidden = NO;
            [pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else {
            add2Label.hidden = NO;
            add2Label.text = @"...";
        }
        sumLabel.text = [NSString stringWithFormat:@"共%ld件", [self.packageInfo.productArray count]];
        moneyLabel.text = [NSString stringWithFormat:@"套餐价:\n￥%@", self.packageInfo.combinePrice];
    }

}

@end
